<%@page language="java" contentType="text/html;charset=utf-8" pageEncoding="utf-8" isELIgnored="false"%>
<%@page import="java.util.List" %>
<%@page import="com.gmail.huxiaowi.cblog.entity.Category" %>
<%@page import="com.gmail.huxiaowi.cblog.util.JCacheUtils" %>
<%@ page import="com.gmail.huxiaowi.cblog.consts.CBlogConsts" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<script type="text/javascript" src="/components/jquery.js"></script>
<script type="text/javascript">
    function showUpdateForm(id, bol) {
        $("#" + id + "_1").css("display", (bol == 1) ? "none" : "");
        $("#" + id + "_2").css("display", (bol == 1) ? "" : "none");
    }

    function deleteCategory(id) {
        
        $.ajax({
            type: "POST",
            url: "/delete/category.do",
            data: "id=" + id,
            success: function(msg) {
                if (msg == "ok") {
                    alert("删除成功！");
                } else {
                    alert("删除失败: " + msg);
                }
            }
        });
    }

    function addCategory() {
        var name = $.trim($("#name").val());
        if (name == "") {
            alert("分类名不能为空");
            $("#name").get(0).focus();
            return ;
        }

        $.ajax({
            type: "POST",
            url: "/create/category.do",
            data: "name=" + name,
            success: function(msg) {
                if (msg == "ok") {
                    window.location.reload();
                }else {
                    alert("添加失败: " + msg);
                }
            }
        });
    }

    function updateCategory(id) {
        var name = $.trim($("#" + id).val());
        if (name == "") {
            alert("分类名不能为空");
            $("#" + id).get(0).focus();
            return ;
        }

        $.ajax({
            type: "POST",
            url: "/update/category.do",
            data: $.param({"id": id, "name": name}),
            success: function(msg) {
                if (msg == "ok") {
                    window.location.reload();
                } else {
                    alert("添加失败: " + msg);
                }
            }
        });
    }
</script>

<%
List<Category> categorys =  (List<Category>) JCacheUtils.get(CBlogConsts.CACHED_KEY_CATALOGS);
request.setAttribute("categorys", categorys);
%>
<div>
    分类名：<input id="name" type="text"/><a href="javascript:addCategory()">添加</a>
</div>
<div>
    <table width="500px">
        <tr>
            <td width="250"></td>
            <td width="125"></td>
            <td width="125"></td>
        </tr>
        <c:forEach items="${categorys}" var="category" varStatus="vs">
            <tr id="${category.id}_1">
                <td>${category.name }</td>
                <td><a href="javascript:showUpdateForm('${category.id}', 1)">修改</a></td>
                <td><a href="javascript:deleteCategory('${category.id }')">删除</a></td>
            </tr>
            <tr id="${category.id}_2" style="display:none">
                <td><input id="${category.id }" type="text" value="${category.name }"/></td>
                <td><a href="javascript:updateCategory('${category.id }')">提交</a></td>
                <td><a href="javascript:showUpdateForm('${category.id }', 0)">取消</a></td>
            </tr>
        </c:forEach>
    </table>
</div>